﻿using Mirror;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HighEnergyCrystalGunery : Ship
{
    public HighEnergyCrystalBullet m_HighEnergyCrystalBullet;
    public HighEnergyCrystalBullet m_CurrentBullet;
    public GameObject m_ChargePoint;
    public AudioSource m_StartChargeAudio;
    public AudioSource m_EndChargeAudio;
    public AudioSource m_ChargeAudio;
    float m_ChargeEnergy=0;
    float m_ChargeTimeCount = 0;
    bool m_ChargeFlag = false;

    bool FirstActive = false;
    protected new void Start()
    {
        base.Start();
    }

    // Update is called once per frame
    protected new void Update()
    {
        base.Update();
        if (UtilsGame.IsOnline() && !UtilsGame.IsServer(gameObject))
            return;

        if (m_ChargeFlag == false)
        {
            m_ChargeTimeCount = Mathf.Clamp(m_ChargeTimeCount - Time.deltaTime, 0, 0.5f);
        }
        m_ChargeFlag = false;
        if(m_ChargeTimeCount == 0 && m_ChargeEnergy > 0)
        {
            m_ChargeEnergy = 0;
            m_ChargeTimeCount = 0;
            if (m_CurrentBullet)
            {
                m_CurrentBullet.FlyDirection = transform.up;
                m_CurrentBullet.FlyVelocity = 10;
                m_CurrentBullet.WeaponClassName = "none";
                m_CurrentBullet.WeaponType = Weapon.DamageType.TYPE_ENERGY;
                m_CurrentBullet.Ship = this.gameObject;
                m_CurrentBullet.m_Influence = m_ShipStruct.Influence;
                m_CurrentBullet.m_IsEmit = true;
                m_CurrentBullet.OnBoomCallback = null;

                AddForce(-transform.up, m_CurrentBullet.Energy / 5);
                if (UtilsGame.IsOnline() && UtilsGame.IsServer(gameObject))
                {
                    RpcEmitHighEnergyBullet(-transform.up, m_CurrentBullet.Energy / 5);
                }
                m_CurrentBullet = null;
                if (m_EndChargeAudio)
                    m_EndChargeAudio.Play();
                if (m_ChargeAudio)
                    m_ChargeAudio.Stop();
            }
        }
        if (m_CurrentBullet)
            m_CurrentBullet.transform.position = m_ChargePoint.transform.position;
    }

    protected override void OnDestroy()
    {
        base.OnDestroy();
        if(m_CurrentBullet)
        {
            if (!BulletPool.get().Recycle(m_CurrentBullet.gameObject))
            {
                Destroy(m_CurrentBullet.gameObject);
            }
        }
    }

    public override void FireUp(int groupIndex)
    {
        if (UtilsGame.IsOnline() && !UtilsGame.IsServer(gameObject))
        {
            CmdSetUseSkill(false);
        }
        else
        {
            UseSkill = false;
        }
    }
    public override void fireByWeaponGourp(int groupIndex)
    {
        if (UtilsGame.IsOnline() && !UtilsGame.IsServer(gameObject))
        {
            if (m_ChargeAudio && m_ChargeAudio.isPlaying == false)
                m_ChargeAudio.Play();
            base.fireByWeaponGourp(groupIndex);
            return;
        }
        if (m_CurrentBullet == null &&
            UseSkill == false)
        {
            LaunchSkill();
            UseSkill = true;
        }
    }
    public override bool LaunchSkillInClient()
    {
        if (!base.LaunchSkillInClient())
            return false;
        if (UtilsGame.IsOnline() && !UtilsGame.IsServer(this.gameObject))
        {
            if (m_ChargeAudio && m_ChargeAudio.isPlaying == false)
                m_ChargeAudio.Play();
        }
        return true;
    }
    public override bool LaunchSkill()
    {
        if (!base.LaunchSkill())
            return false;

        if (m_ShipStruct.Electric < 300)
        {
            MessageShow.Instance.Messaage("能量不足~");
            return false;
        }
        if (m_CurrentBullet == null && m_ShipStruct.Electric > 300)
        {
            m_CurrentBullet = Instantiate(m_HighEnergyCrystalBullet);
            m_CurrentBullet.m_IsEmit = false;
            m_CurrentBullet.Ship = this.gameObject;
            m_CurrentBullet.OnBoomCallback = OnCurrentBulletBoom;
            if(UtilsGame.IsOnline() && UtilsGame.IsServer(this.gameObject))
            {
                UtilsGame.SpawnInServer(m_CurrentBullet.gameObject);
            }
            FirstActive = true;
            if (m_ChargeAudio && m_ChargeAudio.isPlaying == false)
                m_ChargeAudio.Play();
        }
        return true;

    }
    public override bool PressSkill()
    {
        float calcEnergy = m_ShipStruct.m_ElectricPower * Time.deltaTime;
        if (FirstActive)
            calcEnergy = 300;
        FirstActive = false;

        if (calcEnergy > m_ShipStruct.Electric)
        {
            m_ChargeTimeCount = 0;
            return true;
        }
        m_ChargeFlag = true;
        m_ChargeTimeCount = 0.5f;
        if (m_CurrentBullet == null)
            return false;
        m_ChargeEnergy += calcEnergy;
        m_ShipStruct.Electric -= calcEnergy;
        if (m_CurrentBullet)
            m_CurrentBullet.Energy = m_ChargeEnergy;
        return true;
    }

    void OnCurrentBulletBoom()
    {
        m_CurrentBullet.OnBoomCallback = null;
        m_CurrentBullet.Death = true;
        m_CurrentBullet.gameObject.SetActive(false);
        m_CurrentBullet = null;
        if (m_ChargeAudio)
            m_ChargeAudio.Stop();
        UseSkill = false;
        m_ChargeEnergy = 0;
    }

    [ClientRpc]
    void RpcEmitHighEnergyBullet(Vector3 emitDirection, float force)
    {
        if(UtilsGame.IsServer(gameObject))
            return;
        if (m_EndChargeAudio)
            m_EndChargeAudio.Play();
        if (m_ChargeAudio)
            m_ChargeAudio.Stop();
    }
}
